#include "MEFXMLParser.h"
#include "MEFStringHash.h"
#include <irrlicht.h>
using namespace irr;
using namespace io;

CMEFXMLParser::CMEFXMLParser()
{
}

CMEFXMLParser::~CMEFXMLParser()
{
}

void CMEFXMLParser::ParseFile(const CMEFString& aFilename, CMEFXMLParserObserver& aObserver)
{
	// create the reader using one of the factory functions
	IrrXMLReader* xml = createIrrXMLReader(aFilename.c_str());

	// parse the file until end reached
	while(xml && xml->read())
	{
		switch(xml->getNodeType())
		{
		case EXN_TEXT:
			// in this xml file, the only text which occurs is the messageText
			aObserver.OnText(xml->getNodeData());
			break;
		case EXN_ELEMENT:
			{
				CMEFStringHash attributes;
				int nrOfAttr = xml->getAttributeCount(); 
				for (int i = 0; i < nrOfAttr; i++)
				{
					attributes.Add(xml->getAttributeName(i), xml->getAttributeValue(i));
				}
				aObserver.OnStartElement(xml->getNodeName(), attributes);
				
				//If the elemnt ends with '/>'
				if(xml->isEmptyElement())
				{
					aObserver.OnEndElement(xml->getNodeName());
				}
			}
			break;
		case EXN_ELEMENT_END:
			aObserver.OnEndElement(xml->getNodeName());
			break;
		case EXN_COMMENT:
		case EXN_CDATA:		
		case EXN_NONE:
		case EXN_UNKNOWN:
		default:
			break;
		}
	}

	// delete the xml parser after usage
	delete xml;

}
